package thread.thread5.t1;

/**
 * 银行账户类
 */
class Account {
	String name;
	float amount;

	public Account(String name, float amount) {
		this.name = name;
		this.amount = amount;
	}

	// 存钱
	public void deposit(float amt) {
		amount += amt;
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	// 取钱
	public void withdraw(float amt) {
		amount -= amt;
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public float getBalance() {
		return amount;
	}
}

/**
 * 账户操作类
 */
class AccountOperator implements Runnable {
	private Account account;

	public AccountOperator(Account account) {
		this.account = account;
	}

	public void run() {
		synchronized (account) {
			account.deposit(500);
			account.withdraw(500);
			System.out.println(Thread.currentThread().getName() + ":" + account.getBalance());
		}
	}
}

public class Demo02 {

	// public static final Object signal = new Object(); // 线程间通信变量
	// 将account改为Demo00.signal也能实现线程同步
	public static void main(String args[]) {
		Account account = new Account("zhang san", 10000.0f);

		final int THREAD_NUM = 5;
		Thread threads[] = new Thread[THREAD_NUM];
		for (int i = 0; i < THREAD_NUM; i++) {
			AccountOperator accountOperator = new AccountOperator(account);
			threads[i] = new Thread(accountOperator, "Thread" + i);
			threads[i].start();
		}
	}
}
